#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Podboy -- A podcast aggregator/player
#
# Copyright (C) 2009-2012 Valéry Febvre <vfebvre@easter-eggs.com>
# http://code.google.com/p/podboy/
#
# This file is part of Podboy.
#
# Podboy is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Podboy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from optparse import OptionParser
import sys

from podboy.libpodcast import Podcast

__all__ = ['import_podcasts']

def import_podcasts(podcasts_file_path, verbose=False):
    podcasts_file = open(podcasts_file_path)
    for line in podcasts_file:
        url = line.strip()
        if not url:
            continue
        if not url.startswith(('http://', 'itpc://', 'feed://', 'itms://')):
            if verbose:
                print 'Skipping invalid URL:', url
            continue
        if verbose:
            print 'Loading podcast:', url
        Podcast(url)
    podcasts_file.close()

def main(argv = None):
    if argv is None:
        argv = sys.argv
    parser = OptionParser('usage: %prog [options] podcasts-file')
    parser.add_option('-v', '--verbose', action = 'store_true', default = False, help = "Display more messages")
    options, arguments = parser.parse_args(argv[1:])
    if len(arguments) != 1:
        parser.error("Incorrect number of arguments")
    import_podcasts(arguments[0], verbose=options.verbose)
    return 0

if __name__ == "__main__":
    sys.exit(main())
